#!/bin/sh
###############################################################################
##  Author    : 陈贰浩
##  Name      : edw_sample.sh
##  Functions : 抽样下载
##  Purpose   : 
##  Revisions or Comments
##  VER        DATE        AUTHOR           DESCRIPTION
##---------  ----------  ---------------  ------------------------------------ 
##  1.0      2019-02-15  陈贰浩           1. CREATED THIS SHELL.
###############################################################################

. ${0%`basename ${0}`}edw_parm.sh
. ${0%`basename ${0}`}edw_func.sh


V_DATA_DATE=$(grep "#V_DATA_DATE#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
V_FREQUENCY=$(grep "#V_FREQUENCY#" ${V_PARM_FILE} | awk -F'=' '{print $2}')
# Define basic parm
V_SCRIPT_NAME=$(basename ${0} | cut -d'.' -f1)
V_OUT_LOGS="${V_SHELL_LOGS}/${V_DATA_DATE}/${V_SCRIPT_NAME}_${V_FREQUENCY}_${V_PARALLEL_CNT}.log"
V_DATA_MONTH=${V_DATA_DATE:0:6}
V_DATA_YEAR=$((${V_DATA_DATE:0:4}+1))
# Define the output flow
exec 4>&1               # screen output
exec 3>>${V_OUT_LOGS}   # script output
exec 2>&3               # error output
exec 1>&3               # standard output


V_SCP_PATH="/home/message"
V_DATA_PATH="${V_SHELL_DATA}/${V_DATA_DATE}"
#从stg层卸数据并压缩，上传到/home/message目录下
V_QUERY="SELECT ORG_CODE FROM dps.org_sample WHERE IS_ACTIVE='1'"
ORG_LIST=$(${V_RUN_DB} -q "${V_QUERY}") || exit 1
for ORG in ${ORG_LIST}
do
  if [ ! "${V_DATA_DATE:4:4}"x = "1331"x ]; then
    FILE_PATH[0]="/user/hive/warehouse/hadoop/stg.db/depb/data_date=${V_DATA_DATE}/data_src_org=${ORG}/DEPB${ORG}${V_DATA_MONTH}.dat"
    FILE_PATH[1]="/user/hive/warehouse/hadoop/stg.db/loab/data_date=${V_DATA_DATE}/data_src_org=${ORG}/LOAB${ORG}${V_DATA_MONTH}.dat"
    FILE_PATH[2]="/user/hive/warehouse/hadoop/stg.db/loaf/data_date=${V_DATA_DATE}/data_src_org=${ORG}/LOAF${ORG}${V_DATA_MONTH}.dat"
    FILE_NAME[0]="DEPB${ORG}"
    FILE_NAME[1]="LOAB${ORG}"
    FILE_NAME[2]="LOAF${ORG}"
  else
    FILE_PATH[0]="/user/hive/warehouse/hadoop/stg.db/depb_13/data_date=${V_DATA_DATE}/data_src_org=${ORG}/DEPBQC${ORG}${V_DATA_YEAR}.dat"
    FILE_PATH[1]="/user/hive/warehouse/hadoop/stg.db/loab_13/data_date=${V_DATA_DATE}/data_src_org=${ORG}/LOABQC${ORG}${V_DATA_YEAR}.dat"
    FILE_PATH[2]="/user/hive/warehouse/hadoop/stg.db/loaf_13/data_date=${V_DATA_DATE}/data_src_org=${ORG}/LOAFQC${ORG}${V_DATA_YEAR}.dat"
    FILE_NAME[0]="DEPBQC${ORG}"
    FILE_NAME[1]="LOABQC${ORG}"
    FILE_NAME[2]="LOAFQC${ORG}"
  fi
  
  LOGGER "INFO" "机构[${ORG}]数据抽取开始."
  for FILE in ${FILE_PATH[@]}
  do
    hadoop fs -get ${FILE} ${V_DATA_PATH} >/dev/null
  done

  for FILE in ${FILE_NAME[@]}
  do
    if [ ! "${V_DATA_DATE:4:4}"x = "1331"x ];then
      if [[ -f ${V_DATA_PATH}/${FILE}${V_DATA_MONTH}.dat ]];then
        zip ${V_DATA_PATH}/${FILE}${V_DATA_MONTH}.zip ${V_DATA_PATH}/${FILE}${V_DATA_MONTH}.dat 
        for HOST in ${V_SCP_HOST[@]}
        do
          scp ${V_DATA_PATH}/${FILE}${V_DATA_MONTH}.zip hadoop@${HOST}:${V_SCP_PATH}/ 
        done
      fi
    else
      if [[ -f ${V_DATA_PATH}/${FILE}${V_DATA_YEAR}.dat ]];then
        zip ${V_DATA_PATH}/${FILE}${V_DATA_MONTH}.zip ${V_DATA_PATH}/${FILE}${V_DATA_YEAR}.dat 
        for HOST in ${V_SCP_HOST[@]}
        do
          scp ${V_DATA_PATH}/${FILE}${V_DATA_MONTH}.zip hadoop@${HOST}:${V_SCP_PATH}/ 
        done
      fi
    fi
  done
  LOGGER "INFO" "机构[${ORG}]数据抽取完成."

done

END
exit 0
